<script setup>
/**
 * 个人主页视图：
 * - 展示当前登录用户的基本信息（用户名、角色、邮箱）
 * - 未登录则重定向到 /login?redirect=/profile
 * - 路由 meta.hideBanner 用于隐藏横幅
 */
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { getCurrentUser, isUserLoggedIn } from '../services/dataService'

const router = useRouter()
const user = ref(null)

onMounted(() => {
  if (!isUserLoggedIn()) {
    router.replace({ path: '/login', query: { redirect: '/profile', mode: 'login' } })
  } else {
    user.value = getCurrentUser()
  }
})
</script>

<template>
  <div class="profile-container" v-if="user">
    <div class="profile-card">
      <div class="profile-header">
        <div class="avatar">{{ user.username?.charAt(0).toUpperCase() || 'U' }}</div>
        <div class="info">
          <h2 class="name">{{ user.username }}</h2>
          <p class="role">{{ user.role ? '角色：' + user.role : '角色：用户' }}</p>
          <p v-if="user.email" class="email">邮箱：{{ user.email }}</p>
        </div>
      </div>

      <div class="profile-body">
        <h3 class="section-title">账户信息</h3>
        <ul class="detail-list">
          <li>用户名：{{ user.username }}</li>
          <li>角色：{{ user.role || 'user' }}</li>
          <li v-if="user.email">邮箱：{{ user.email }}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<style scoped>
.profile-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.profile-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 720px;
  padding: 32px;
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 24px;
}

.avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: #8E8C41;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: 700;
}

.info .name {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.info .role, .info .email {
  margin: 6px 0 0 0;
  color: #666;
  font-size: 14px;
}

.profile-body {
  margin-top: 16px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 12px 0;
}

.detail-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px 20px;
}

.detail-list li {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 14px;
  color: #333;
}

@media (max-width: 768px) {
  .profile-card {
    padding: 24px 20px;
    max-width: 96%;
  }
  
  .detail-list {
    grid-template-columns: 1fr;
  }
}
</style>